package ttOrder.controller;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ttOrder.model.po.Food;
import ttOrder.service.FoodServiceImpl;
import ttOrder.util.CommonMessageResultVO;

@Controller
@RequestMapping("/ttOrder")
public class foodController {
	
	@Resource
	private FoodServiceImpl foodService;

	@ResponseBody
	@RequestMapping("/foodlist")
	public List<Food> foodlist(Model model){
		List<Food> foods = foodService.getAll();
		model.addAttribute("foods", foods);
		return foods;
	}
	
	@ResponseBody 
	@RequestMapping(value = "/addFood", method = RequestMethod.POST)
	public CommonMessageResultVO addFood(Model model,Food food){
		     
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try{
			foodService.insert(food);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		vo.setSuccess(true);
		System.out.println(vo.getSuccess());
		return vo;
	} 
	
	
	@ResponseBody 
	@RequestMapping(value = "/editFood", method = RequestMethod.POST)
	public CommonMessageResultVO editFood(Model model,Food food){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try{
			foodService.update(food);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		vo.setSuccess(true);
		return vo;
	}
	
	@ResponseBody 
	@RequestMapping(value = "/delFood", method = RequestMethod.POST)
	public CommonMessageResultVO delFood(Model model,Integer[] ids){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		try{
			for(int i = 0 ; i<ids.length ; i++){
				Food food = new Food();
				food.setId(ids[i]);
				foodService.deleteByKey(food);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		vo.setSuccess(true);
		return vo;
	}
}
